<?php
namespace app\controller\api\v1;

use app\BaseController;
use app\constant\App;
use app\Request;
use app\service\teach\QiangzhiService;

class Teach extends BaseController
{
    private $type; // 教务系统类型
    private $client; // 客户端

    public function __construct()
    {
        $appId = request()->header('App-Id');
        $this->type = App::TEACH_APP_LIST[$appId] ?? 2;
        $url = App::TEACH_APP_URL_LIST[$appId];

        switch ($this->type) {
            case App::ZHENGFANG_TEACH_APP:
            case App::QIANGZHI_TEACH_APP:
                $this->client = new QiangzhiService($url);
        }
    }

    /*
     * 授权登录
     */
    public function auth(Request $request)
    {
        switch ($this->type) {
            case App::ZHENGFANG_TEACH_APP:
            case App::QIANGZHI_TEACH_APP:
                $number = $request->param("number");
                $password = $request->param("password");
                if (!$number || !$password) {
                    return $this->error("请填写账号和密码");
                }

                $res = $this->client->login($number, $password);
                if ($res['status'] == 0) {
                    $data = $this->client->getPersonInfo();
                    return $this->success($data);
                }
                return $this->error($res['msg']);
        }
        return;
    }

    /*
     * 个人信息
     */
    public function personInfo(Request $request)
    {
        switch ($this->type) {
            case App::ZHENGFANG_TEACH_APP:
            case App::QIANGZHI_TEACH_APP:
                return $this->success($this->client->getPersonInfo());
        }
        return;
    }

    /*
     * 成绩信息条件
     */
    public function scoreQuery(Request $request)
    {
        switch ($this->type) {
            case App::ZHENGFANG_TEACH_APP:
            case App::QIANGZHI_TEACH_APP:
                return $this->success($this->client->getScoreQuery());
        }
        return;
    }

    /*
     * 成绩信息
     */
    public function scoreInfo(Request $request)
    {
        switch ($this->type) {
            case App::ZHENGFANG_TEACH_APP:
            case App::QIANGZHI_TEACH_APP:
                $xq = $request->param("xq", ""); // 学期
                $type = $request->param("type", ""); // 课程性质
                $name = $request->param("name", ""); // 课程名称
                $show = $request->param("show", "all"); // 显示类型
                return $this->success($this->client->getScoreInfo($xq, $type, $name, $show));
        }
        return;
    }

    /*
     * 课表信息
     */
    public function courseInfo(Request $request)
    {
        switch ($this->type) {
            case App::ZHENGFANG_TEACH_APP:
            case App::QIANGZHI_TEACH_APP:
                $week = $request->param("week", ""); // 周次
                $xqxn = $request->param("xqxn", ""); // 学期学年
                return $this->success($this->client->getCourseInfo($week, $xqxn));
        }
        return;
    }

    /*
     * 学生评价
     */
    public function reviewFind(Request $request)
    {
        switch ($this->type) {
            case App::ZHENGFANG_TEACH_APP:
            case App::QIANGZHI_TEACH_APP:
                return $this->success($this->client->getReviewFind());
        }
        return;
    }

    /*
     * 评价列表
     */
    public function reviewList(Request $request)
    {
        switch ($this->type) {
            case App::ZHENGFANG_TEACH_APP:
            case App::QIANGZHI_TEACH_APP:
                $url = $request->param("url"); // url
                if (!$url) {
                    return $this->error("选择有误");
                }
                return $this->success($this->client->getReviewList($url));
        }
        return;
    }

    /*
     * 评价详情
     */
    public function reviewDetail(Request $request)
    {
        switch ($this->type) {
            case App::ZHENGFANG_TEACH_APP:
            case App::QIANGZHI_TEACH_APP:
                $url = $request->param("url"); // url
                if (!$url) {
                    return $this->error("参数有误");
                }
                return $this->success($this->client->getReviewDetail($url));
        }
        return;
    }

    /*
     * 提交学生评价
     */
    public function reviewSubmit(Request $request)
    {
        switch ($this->type) {
            case App::ZHENGFANG_TEACH_APP:
            case App::QIANGZHI_TEACH_APP:
                $url = $request->param("url"); // url
                $issumbit = $request->param("issumbit", 1); // 提交类型 0-保存 1-提交
                $pj0601id_3 = $request->param("pj0601id_3"); // pj0601id_3
                $pj0601id_4 = $request->param("pj0601id_4"); // pj0601id_4
                $pj0601id_2 = $request->param("pj0601id_2"); // pj0601id_2
                $pj0601id_5 = $request->param("pj0601id_5"); // pj0601id_5
                $jynr = $request->param("jynr"); // jynr
                if (!$url || !$pj0601id_3 || !$pj0601id_4 || !$pj0601id_2 || !$pj0601id_5 || !$jynr) {
                    return $this->error("参数有误");
                }
                return $this->success($this->client->getReviewEdit($url, $issumbit, $pj0601id_3, $pj0601id_4, $pj0601id_2, $pj0601id_5, $jynr));
        }
        return;
    }
}
